package com.isoft.demo.controller;

import com.isoft.demo.common.ResponseData;
import com.isoft.demo.entity.Address;
import com.isoft.demo.entity.Orders;
import com.isoft.demo.service.OrderService;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @version 1.0
 * @ClassName:OrderController
 * @Description: TODO
 * @Author guowei
 * @Date:2021/6/26 10:45
 */
@RestController
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService service;

    @GetMapping("/addorder")
    public ResponseData addOrder(Orders orders){
//        获取当前系统时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orders.setOtime(format.format(date));
//        总价钱
        double sum = orders.getUnitprice() * orders.getPweight();
        orders.setPsum(sum);

        ResponseData rs = service.add(orders);
        return rs;
    }

    @GetMapping("/searchOrders")
    public ResponseData searchOrders(){
        ResponseData rs = service.search();
        return rs;
    }

    @GetMapping("/searchOrders/ById")
    public ResponseData searchOrdersById(int oid){
        ResponseData rs = service.searchById(oid);
        return rs;
    }

    @PostMapping("/searchAddress")
    public ResponseData searchAddress(){
        ResponseData rs = service.searchAddress();
        return rs;
    }

    @PostMapping("/addAddress")
    public ResponseData addAddress(Address address,int cid){
        address.setCid(cid);
        ResponseData rs = service.addAddress(address);
        return rs;
    }

    @GetMapping("/queryByCid")
    public ResponseData queryByCid(int cid){
        ResponseData rs = service.queryByCid(cid);
        return rs;
    }

    @GetMapping("/sumByCid")
    public ResponseData sumByCid(int cid){
        ResponseData rs = service.sumByCid(cid);
        return rs;
    }
}
